package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired
    SetmealService setmealService;

    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(value = "setmeal",key = "#setmealDTO.categoryId")
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.insertSetmeal(setmealDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result selectSetmealPage(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult = setmealService.selectSetmealPage(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("套餐停售/启售")
    @CacheEvict(value = "setmeal",allEntries = true)
    public Result updateSetmealStatus(@PathVariable Integer status , @Param("id") Long id){
        setmealService.updateSetmealStatus(status,id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result selectSetmealById(@PathVariable Long id){
        Setmeal setmeal = setmealService.selectSetmealById(id);
        return Result.success(setmeal);
    }

    @PutMapping
    @ApiOperation("套餐的修改")
    @CacheEvict(value = "setmeal",allEntries = true)
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("根据Ids删除套餐")
    @CacheEvict(value = "setmeal",allEntries = true)
    public Result deleteSetmealByIds(@RequestParam List<Long> ids){
        setmealService.deleteSetmealByIds(ids);
        return Result.success();
    }

}
